package com.deer.wms.system.manage.web;

import com.deer.wms.intercept.annotation.Authority;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.system.manage.model.address.ReceivingAddress;
import com.deer.wms.system.manage.model.address.ReceivingAddressCreate;
import com.deer.wms.system.manage.model.address.ReceivingAddressCriteria;
import com.deer.wms.system.manage.model.address.ReceivingAddressDto;
import com.deer.wms.system.manage.service.ReceivingAddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by guo on 2019/11/13.
*/
@Api(description = "客户收件信息api接口")
@RestController
@RequestMapping("/receiving/addresss")
public class ReceivingAddressController {

    @Autowired
    private ReceivingAddressService receivingAddressService;


    @ApiOperation(value = "新增客户收件地址", notes = "新增客户收件地址")
    @Authority
    @ApiImplicitParams( { @ApiImplicitParam( name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true ) } )
    @PostMapping("/insert")
    public Result add(@RequestBody ReceivingAddressCreate create, @ApiIgnore @User CurrentUser currentUser) {
        receivingAddressService.createReceivingAddress(create,currentUser);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        receivingAddressService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody ReceivingAddress receivingAddress) {

        if(receivingAddress.getState() ==1) {
            List<ReceivingAddressDto> list = receivingAddressService.findByCustomerId(receivingAddress.getCustomerId());
            for (ReceivingAddress receivingAddress1 : list){
                receivingAddress1.setState(0);
                receivingAddressService.updateById(receivingAddress1);
            }
        }
        receivingAddressService.updateById(receivingAddress);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        ReceivingAddress receivingAddress = receivingAddressService.getById(id);
        return ResultGenerator.genSuccessResult(receivingAddress);
    }


    @ApiOperation(value = "客户收件地址信息", notes = "客户收件地址信息")
    @Authority
    @ApiImplicitParams( { @ApiImplicitParam( name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true ) } )
    @GetMapping("/list")
    public Result list(ReceivingAddressCriteria criteria , @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<ReceivingAddressDto> list = receivingAddressService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
